﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 使用模板的目的是实现让子类继承基类的单例模式
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseManager<T> : MonoBehaviour where T: MonoBehaviour
{
    #region 单例模式

    private static T _instance;

    public static T Instance
    {
        get
        {
            return _instance;
        }

        set
        {
            if(_instance==null)
            {
                _instance = value;
            }
            else
            {
                DestroyImmediate(value.gameObject);
            }
            
        }
    }

    [EnumFlags]
    ///该Manager会依据哪些游戏状态而发生反应
    public EVENT_TYPE Event_Types;

    #endregion

    #region 函数

    protected void Awake()
    {
        if (Instance == null)
            DontDestroyOnLoad(this.gameObject);
        Instance = this as T;
    }

    public void ManagerToDead()
    {
        DestroyImmediate(this);
    }

    private void Start()
    {
        if (EventManager.Instance == null) return;
        foreach(EVENT_TYPE t in Enum.GetValues(typeof(EVENT_TYPE))){
            if((t&Event_Types)==t)
                EventManager.Instance.AddListener(t, OnMyEvent);
        }
    }

    protected abstract void OnMyEvent(EVENT_TYPE Event_Type,
        Component Sender, object Param = null);

    #endregion


}
